package com.cnap.multiclouldmgmt.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;

import javax.sql.DataSource;
import java.util.Objects;

/**
 * 配置数据库链接
 */
@Configuration
// 配置mybatis的接口类放的地方
@MapperScan(basePackages = "com.cnap.multiclouldmgmt.repository.master", sqlSessionFactoryRef = "cnapSqlSessionFactory")
public class CnapDataSourceConfig {

    // 将这个对象放入Spring容器中
    @Bean(name = "CnapDataSource")
    // 表示这个数据源是默认数据源
    @Primary
    // 读取application.properties中的配置参数映射成为一个对象
    // prefix表示参数的前缀
    @ConfigurationProperties(prefix = "spring.datasource.master")
    public DataSource getDateSourceCnap() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "cnapSqlSessionFactory")
    // 表示这个数据源是默认数据源
    @Primary
    // @Qualifier表示查找Spring容器中名字为test1DataSource的对象
    public SqlSessionFactory cnapSqlSessionFactory(@Qualifier("CnapDataSource") DataSource datasource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
                // 设置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver().getResources("classpath*:mappers/master/*.xml"));
        SqlSessionFactory factory = bean.getObject();
        if (Objects.nonNull(factory)) {
            org.apache.ibatis.session.Configuration configuration = factory.getConfiguration();
            // 下划线转驼峰
            if (Objects.nonNull(configuration)) {
                configuration.setMapUnderscoreToCamelCase(true);
            }
        }
        return bean.getObject();
    }

    @Bean("cnapSqlSessionTemplate")
    // 表示这个数据源是默认数据源
    @Primary
    public SqlSessionTemplate cnapSqlSessionTemplate(
            @Qualifier("cnapSqlSessionFactory") SqlSessionFactory sessionfactory) {
        return new SqlSessionTemplate(sessionfactory);
    }

    @Bean(name = "cnapDataSourceInitializer")
    public DataSourceInitializer dataSourceInitializer(@Qualifier("CnapDataSource") DataSource dataSource) {
        ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
        resourceDatabasePopulator.addScript(new ClassPathResource("db/multi-cloud-create-table.sql"));
        resourceDatabasePopulator.addScript(new ClassPathResource("db/master-data-prod.sql"));
        DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
        dataSourceInitializer.setDataSource(dataSource);
        dataSourceInitializer.setDatabasePopulator(resourceDatabasePopulator);
        return dataSourceInitializer;
    }

}
